package org.rambadger.command.resource;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.yammer.dropwizard.auth.Auth;
import com.yammer.dropwizard.views.View;
import org.axonframework.commandhandling.gateway.CommandGateway;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import org.mongojack.DBCursor;
import org.mongojack.DBSort;
import org.mongojack.JacksonDBCollection;
import org.rambadger.command.auth.AuthUser;
import org.rambadger.command.axon.JCollection;
import org.rambadger.command.command.CreateTrack;
import org.rambadger.command.representation.Platform;
import org.rambadger.command.representation.User;
import org.rambadger.command.validation.UrlIdentifier;
import org.rambadger.command.view.PlatformView;
import org.rambadger.command.view.UserView;


import javax.annotation.Nullable;
import javax.validation.Valid;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Path("/registertrack")
public class RegisterTrackResource {
    private @Context CommandGateway gateway = null;

    @JCollection(type=Platform.class)
    private JacksonDBCollection<Platform, String> platforms = null;

    public static class TrackSubmissionJSON {

        @Valid @NotEmpty @JsonProperty
        String platformId;

        @Valid @NotEmpty @UrlIdentifier @JsonProperty
        String trackId;
        @Valid @NotEmpty  @JsonProperty
        String trackName;
        @Valid @NotEmpty @JsonProperty
        String description;

    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public void registerTrack(@Auth(required = true) AuthUser user, @Valid TrackSubmissionJSON newTrackData) {

        //TODO: Check formatting of stuff
        if (user.hasAdmin())
        gateway.send(new CreateTrack(newTrackData.platformId, newTrackData.trackId, newTrackData.trackName, newTrackData.description));

    }
    @GET
    @Produces(MediaType.TEXT_HTML)
    public View getTrackList(@Auth(required = true) AuthUser user) {
        DBCursor<Platform> found = platforms.find();
        //int usersTotal = platforms.find().count();
        //System.out.println(usersTotal);
        List<PlatformView> platformsFound = FluentIterable.from(found).transform(new Function<Platform, PlatformView>() {
            @Nullable
            @Override
            public PlatformView apply(@Nullable Platform input) {
                return new PlatformView(null,input);
            }
        }).toList();
        found.close();
        return new RegisterTrackForm(user, platformsFound);
    }

}
class RegisterTrackForm extends View{
    String title = ": Admin Panel";
    boolean area_admin = true;
    boolean hasAdmin = false;
    List<PlatformView> platforms;
    public RegisterTrackForm(@Auth(required = false) AuthUser user, List<PlatformView> platforms){
        super("/registerTrack.mustache");
        this.platforms = platforms;
        this.hasAdmin = user.hasAdmin();
    }
}
